package com.foodplus.www.adapter.shop;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.foodplus.www.bean.good.GoodsListBean;
import com.foodplus.www.foodplus.R;

import java.util.List;

/**
 * Created by My on 2016/10/31.
 */

public class GoodAdapter extends RecyclerView.Adapter {

    private List<GoodsListBean> list;
    private Context context;
    private OnItemClickListener onItemClickListener;

    public GoodAdapter(List<GoodsListBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_good_item,parent,false);
        GoodItemViewHolder holder = new GoodItemViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Glide.with(context)
                .load(list.get(position).getGoods_image_url())
                .placeholder(R.mipmap.bg_morentu)
                .fitCenter()
                .into(((GoodItemViewHolder)holder).iv);
        ((GoodItemViewHolder)holder).title.setText(list.get(position).getGoods_name());
        ((GoodItemViewHolder)holder).pro.setText(list.get(position).getGoods_jingle());
        ((GoodItemViewHolder)holder).price.setText("¥"+list.get(position).getGoods_price());
        ((GoodItemViewHolder)holder).oldprice.setText("原价：¥"+list.get(position).getGoods_marketprice());
        ((GoodItemViewHolder)holder).person.setText("已售："+list.get(position).getGoods_salenum()+"件");



    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class GoodItemViewHolder extends RecyclerView.ViewHolder{

        public ImageView iv;
        public TextView title;
        public TextView pro;
        public TextView price;
        public TextView oldprice;
        public TextView person;

        public GoodItemViewHolder(View itemView) {
            super(itemView);

            iv = (ImageView) itemView.findViewById(R.id.good_item_iv);
            title = (TextView) itemView.findViewById(R.id.good_item_title);
            pro = (TextView) itemView.findViewById(R.id.good_item_pro);
            price = (TextView) itemView.findViewById(R.id.good_item_price);
            oldprice = (TextView) itemView.findViewById(R.id.good_item_oldprice);
            person = (TextView) itemView.findViewById(R.id.good_item_person);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClickListener(getLayoutPosition());
                    }
                }
            });
        }
    }
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }



    public interface OnItemClickListener {
        void onItemClickListener(int position);
    }
}
